package com.zkingsoft.actions.admin;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.config.Dictionary;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.ShowElement;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.ShowBoxService;
import com.zkingsoft.services.ShowElementService;
import com.zkingsoft.services.ShowPointService;

/**
 * @project MatrixCms
 * @package com.zkingsoft.actions.admin
 * @class ShowElementController.java
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2016年4月21日10:31
 * @description 系统按钮管理
 */
@Controller
@RequestMapping(value = "/admin/article")
public class ArticleController extends BaseController {


	@Resource
	private ShowElementService currentService;
	@Resource
	private ShowPointService showPointService;

	@Resource
	private ShowBoxService showBoxService;

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, ShowElement article, PaginationVO pageVo) {
		article.setType(Dictionary.ELEMENT_TYPE_ARTICLE);
		article.setCompanyId(getMe(request).getCompanyId());
		return showList(currentService, article, pageVo);
	}

	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		ShowElement showElement = new ShowElement();
		showElement.setType(Dictionary.ELEMENT_TYPE_ARTICLE);
		return findByModel(currentService, showElement);
	}

	@RequestMapping(value = "/changeShow")
	public @ResponseBody AjaxResult changeShow(Integer flage, String keys, HttpServletRequest request) {
		ShowElement article = new ShowElement();
		article.setIsShow(flage);
		article.setId(Long.parseLong(keys));
		currentService.modify(article);
		String msg = "";
		if (flage == 1) {
			msg = "发布成功";
		} else if (flage == 2) {
			msg = "文章已经撤回";
		}
		return new AjaxResult(AjaxResult.STATUS_OK, null, msg);
	}

	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(ShowElement showElement, HttpServletRequest request) {
		if (showElement.getId() != null) {
			if(showElement.getVotime()!=null){
				showElement.setTime(DateUtils.stringToDate(showElement.getVotime(), "yyyy-MM-dd"));
			}
			return modify(currentService, showElement, "文章");
		} else {
			showElement.setType(Dictionary.ELEMENT_TYPE_ARTICLE);
			if(showElement.getVotime()==null){
				showElement.setTime(new Date());
			}else{
				showElement.setTime(DateUtils.stringToDate(showElement.getVotime(), "yyyy-MM-dd"));
			}
			showElement.setCompanyId(getMe(request).getCompanyId());
			return add(currentService, showElement, "文章");
		}
	}

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		List<String> sourceList = Arrays.asList("原创", "转载", "翻译");
		request.setAttribute("sourceList", sourceList);
		ShowElement showElement;
		if (id != null) {
			showElement = currentService.findById(id);
			request.setAttribute("obj", showElement);
		}
		return "admin/article-form";
	}

}